package slaythespire;

import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.red.Barricade;
import com.megacrit.cardcrawl.cards.red.BodySlam;
import com.megacrit.cardcrawl.cards.red.ShrugItOff;

import javax.swing.*;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.HashMap;

public class CardsUI extends JDialog {
    private JPanel contentPane;
    private JCheckBox card1CheckBox;
    private JTextField card1Text;
    private JCheckBox card2CheckBox;
    private JTextField card2Text;
    private JCheckBox card3CheckBox;
    private JTextField card3Text;

    public CardsUI() {
        setContentPane(contentPane);
        setModal(true);
        card1CheckBox.addItemListener(checkBoxItemListener);
        card2CheckBox.addItemListener(checkBoxItemListener);
        card3CheckBox.addItemListener(checkBoxItemListener);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
                StsHelper.cardList.clear();
            }

            @Override
            public void windowClosing(WindowEvent e) {
                StsHelper.cardList.clear();
            }

            @Override
            public void windowClosed(WindowEvent e) {

            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {

            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        });

        cardMap.clear();
        cardMap.put("Body Slam", new BodySlam());
        cardMap.put("Shrug It Off", new ShrugItOff());
        cardMap.put("Barricade", new Barricade());
    }

    protected ItemListener checkBoxItemListener = e -> {
        boolean isChecked = e.getStateChange() == 1;
        JCheckBox cb = (JCheckBox) e.getItem();
        JTextField text = null;
        String id;
        switch (cb.getText()) {
            case "Card1":
                id = "Body Slam";
                text = card1Text;
                break;
            case "Card2":
                id = "Shrug It Off";
                text = card2Text;
                break;
            case "Card3":
                id = "Barricade";
                text = card3Text;
                break;
            default:
                id = null;
        }
        if (id != null) {
            if (isChecked) {
                addCardID(id,text);
            } else {
                removeCardID(id, text);
            }
        }
    };

    private HashMap<String, AbstractCard> cardMap = new HashMap<>();

    private boolean isCardExisted(String id) {
        for (AbstractCard card : StsHelper.cardList) {
            if (card.cardID.equals(id)) {
                return true;
            }
        }
        return false;
    }

    private void addCardID(String id, JTextField text) {
        if (!isCardExisted(id)) {
            AbstractCard card = cardMap.get(id);
            if (card != null) {
                StsHelper.cardList.add(card);
                text.setText(id);
            } else {
                text.setText(id + " is null");
            }
        } else {
            text.setText("addCardID error");
        }
    }

    private void removeCardID(String id, JTextField text) {
        if (isCardExisted(id)) {
            AbstractCard card = cardMap.get(id);
            if (card != null) {
                StsHelper.cardList.remove(card);
                text.setText("");
            } else {
                text.setText(id + " is null");
            }
        } else {
            text.setText("removeCardID error");
        }
    }

    public static void main(String[] args) {
        JDialog dialog = new CardsUI();
        dialog.pack();
        dialog.setVisible(true);
        dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        System.out.println("ui end");
    }
}
